﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Admin
{
    public partial class ShopSetting : System.Web.UI.Page
    {
        CustomDataContext db = new CustomDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMyData(0);
                int Totals = TotalPageAmount();
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";
            }
        }
        #region 绑定数据及翻页
        private void BindMyData(int PageIndex)
        {
            int skipAccount = PageIndex * 10;
            var MyQuery = (from a in db.TCrm_JC_Parameter
                           select a).Skip(skipAccount).Take(10);
            GridView_Shops.DataSource = MyQuery;
            GridView_Shops.DataBind();

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            MyQuery = (from a in db.TCrm_JC_Parameter
                       select a).Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
            Label_Total.Text = "共" + pages.ToString();
            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";
            BindMyData(pageIndx);

        }

        #endregion
        //增加按钮的事件
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            Panel_Shop.Visible = false;
            TextBox_ShopID.Text = "";
            TextBox_ShopName.Text = "";
            TextBox_AppID.Text = "";
            TextBox_AppSecret.Text = "";
            CheckBox_TB.Checked = false;
            TextBox_Session.Text = "";
            Panel_Edit.Visible = true;
            TextBox_ShopID.Enabled = true;
            EditFlag.Value = "add";
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('增加','Panel_Edit');</script>", false);
        }
        //保存的按钮
        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {
            SessionTrans st=new SessionTrans();
            string appsecret = TextBox_AppSecret.Text;
            string sessionkey = TextBox_Session.Text;
            if (EditFlag.Value == "add")
            {
                TCrm_JC_Parameter myrow = new TCrm_JC_Parameter();
                myrow.ShopID = TextBox_ShopID.Text;
                myrow.IsTB = CheckBox_TB.Checked;
                myrow.ShopName = TextBox_ShopName.Text;
                myrow.AppID = TextBox_AppID.Text;
                myrow.AppSecret = appsecret;
                myrow.SessionKey = st.TranstoFalse(appsecret,sessionkey);
                db.TCrm_JC_Parameter.InsertOnSubmit(myrow);


                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加成功'); </script>");
                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Shop.Visible = true;
                    BindMyData(0);
                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加失败'); </script>");
                }


            }
            else if (EditFlag.Value == "edit")
            {
                string ShopID = TextBox_ShopID.Text;
                var myEditQuery = (from a in db.TCrm_JC_Parameter where a.ShopID == ShopID select a).Single();

                myEditQuery.ShopID = TextBox_ShopID.Text;
                myEditQuery.ShopName = TextBox_ShopName.Text;
                myEditQuery.AppID = TextBox_AppID.Text;
                myEditQuery.AppSecret = appsecret;
                myEditQuery.IsTB = CheckBox_TB.Checked;
                myEditQuery.SessionKey = st.TranstoFalse(appsecret, sessionkey);
                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑成功'); </script>");
                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Shop.Visible = true;
                    TextBox_ShopID.Enabled = true;
                    BindMyData(0);

                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑失败');</script>");
                }

            }

        }
        //取消的按钮
        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            EditFlag.Value = "0";
            Panel_Edit.Visible = false;
            Panel_Shop.Visible = true;
            if (EditFlag.Value == "edit")
            {
                TextBox_ShopID.Enabled = true;

            }

        }
        protected void Button_Delete_Command(object sender, CommandEventArgs e)
        {
            string ShopID = e.CommandArgument.ToString();
            var query = from a in db.TCrm_KF_Orders where a.seller_nick == ShopID select a;
            if (query.Count() != 0)
            {
                alert = "<script>alert('该店铺存在订单，不能删除;');</script>";
            }
            else
            {
                var queryshop = from a in db.TCrm_JC_Parameter where a.ShopID == ShopID select a;
                db.TCrm_JC_Parameter.DeleteAllOnSubmit(queryshop);
                db.SubmitChanges();
                alert = "<script>alert('成功删除该店铺;');</script>";
            }
        }
        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            SessionTrans st = new SessionTrans();
            string ShopID = e.CommandArgument.ToString();
            var myEditQuery = (from a in db.TCrm_JC_Parameter
                               where a.ShopID == ShopID
                               select a
                               ).Single();
            TextBox_ShopID.Text = myEditQuery.ShopID;
            TextBox_ShopName.Text=myEditQuery.ShopName;
            CheckBox_TB.Checked = Convert.ToBoolean(myEditQuery.IsTB);
            TextBox_AppID.Text = myEditQuery.AppID;
            TextBox_AppSecret.Attributes.Add("value", myEditQuery.AppSecret);
            TextBox_Session.Attributes.Add("value", st.TranstoTrue(myEditQuery.SessionKey));
            TextBox_ShopID.Enabled = false;
            EditFlag.Value = "edit";
            Panel_Edit.Visible = true;
            Panel_Shop.Visible = false;
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('编辑','Panel_Edit');</script>", false);
        }
    }
}
